package com.wgchao.authmanager.application.service;

import com.wgchao.authmanager.domain.model.Handle;
import com.wgchao.authmanager.domain.model.MenuResource;
import com.wgchao.authmanager.domain.model.Project;
import com.wgchao.authmanager.domain.params.*;
import com.wgchao.authmanager.domain.repository.HandleRepository;
import com.wgchao.authmanager.domain.repository.ProjectRepository;
import com.wgchao.authmanager.infrastructure.exception.ObjectNotExistException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @ClassName: ResourceService
 * @author: wgchao
 * @createTime: 2020/4/27 2:26 PM
 * @Description: 资源管理（应用服务）
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ResourceService {
    @Autowired
    private ProjectRepository projectRepository;
    @Autowired
    private HandleRepository handleRepository;

    /**
     * 新增项目资源
     * @param creator
     */
    public void addProject(ProjectCreator creator){
        Project project = Project.addProject(creator);
        projectRepository.add(project);
    }

    /**
     * 修改项目资源信息
     * @param identify
     * @param updator
     */
    public void resiveProjectInfo(String identify, ProjectUpdator updator){
        Project project = projectRepository.get(identify);
        if(project == null){
            throw new ObjectNotExistException("项目不存在");
        }
        project.resiveInfo(updator);
        projectRepository.add(project);
    }

    /**
     * 删除项目资源
     * @param identify
     */
    public void removeProject(String identify){
        Project project = projectRepository.get(identify);
        if(project == null){
            throw new ObjectNotExistException("项目不存在");
        }
        projectRepository.remove(project, null);
    }

    /**
     * 向项目资源添加菜单资源
     * @param projectIdentify
     * @param creator
     */
    public void addMenuToProject(String projectIdentify, MenuResourceCreator creator){
        Project project = projectRepository.get(projectIdentify);
        if(project == null){
            throw new ObjectNotExistException("项目不存在");
        }
        project.addMenu(creator);
        projectRepository.add(project);
    }

    /**
     * 修改菜单资源信息
     * @param projectIdentify
     * @param menuIdentify
     * @param updator
     */
    public void resiveMenuInfo(String projectIdentify, String menuIdentify, MenuResourceUpdator updator){
        Project project = projectRepository.get(projectIdentify);
        if(project == null){
            throw new ObjectNotExistException("项目不存在");
        }
        project.resiveMenu(menuIdentify, updator);
        projectRepository.add(project);
    }

    /**
     * 移除菜单资源
     * @param menuIdentify
     */
    public void removeMenu(String projectIdentify, String menuIdentify){
        Project project = projectRepository.get(projectIdentify);
        if(project == null){
            throw new ObjectNotExistException("项目不存在");
        }
        project.removeMenu(menuIdentify);
        projectRepository.add(project);
    }

    /**
     * 创建操作资源
     * @param creator
     */
    public void addHandle(HandleCreator creator){
        Handle handle = Handle.addHandle(creator);
        handleRepository.add(handle);
    }

    /**
     * 修改操作资源信息
     * @param identify
     * @param updator
     */
    public void resivehandleInfo(String identify, HandleUpdator updator){
        Handle handle = handleRepository.get(identify);
        if(handle == null){
            throw new ObjectNotExistException("操作资源不存在");
        }
        handle.resiveInfo(updator);
        handleRepository.add(handle);
    }

    /**
     * 删除操作资源
     * @param identify
     */
    public void removeHandle(String identify) {
        Handle handle = handleRepository.get(identify);
        if(handle == null){
            throw new ObjectNotExistException("操作资源不存在");
        }
        handleRepository.remove(handle, null);
    }
}
